package Orm.jdbc.base;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 使用ResultSet对象进行游标移动的操作，数据更新，数据添加的操作
 */
public class Study05_MoveResultSet {
    public static void main(String[] args) {
        //驱动加载
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("成功mysql加载驱动！！");
        }catch (Exception e){
            e.printStackTrace();
        }
        //和数据库建立连接
        String url="jdbc:mysql://localhost:3306/world?&useSSL=false&serverTimezone=UTC";
        String username="root";
        String password="root";
        try {
            Connection conn = DriverManager.getConnection(url,username,password);
            System.out.println("成功获取数据库连接！！");
            Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            System.out.println("成功获取数据库操作对象");
            String sql = "select * from city";
            ResultSet rs = stmt.executeQuery(sql);
            System.out.println("成功执行sql语句：\"select * from city\" ");
            System.out.println("id|name|countryCode|");
            while(rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String countryCode = rs.getString("countryCode");
                System.out.println(id +" | "+name+" | "+countryCode);
            }
            System.out.println("数据遍历完毕！！------");
            System.out.println("移动到倒数第一行：");
            rs.previous();
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String countryCode = rs.getString("countryCode");
            System.out.println(id +" | "+name+" | "+countryCode);
            System.out.println("移动到100行：");
            rs.absolute(100);
            id = rs.getInt("id");
            name = rs.getString("name");
            countryCode = rs.getString("countryCode");
            System.out.println(id +" | "+name+" | "+countryCode);
            System.out.println("移动到最后面");
            rs.afterLast();
            rs.previous();
            id = rs.getInt("id");
            name = rs.getString("name");
            countryCode = rs.getString("countryCode");
            System.out.println(id +" | "+name+" | "+countryCode);
            rs.updateString("name","Rafahh");
            rs.updateRow();
            rs.moveToInsertRow();
            rs.updateInt(1,4080);
            rs.updateString(2,"Yanjid");
            rs.updateString(3,"PSE");
            rs.updateString(4,"Rafah");
            rs.updateInt(5,92021);
            rs.insertRow();
            rs.moveToCurrentRow();
            id = rs.getInt("id");
            name = rs.getString("name");
            countryCode = rs.getString("countryCode");
            System.out.println(id +" | "+name+" | "+countryCode);
            rs.close();
            stmt.close();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
